package com.tencent.wxcloudrun.vo.response.card;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.tencent.wxcloudrun.domain.TUser;
import com.tencent.wxcloudrun.util.DateUtils;
import com.tencent.wxcloudrun.util.RedisBusiUtil;
import org.springframework.util.CollectionUtils;

import com.tencent.wxcloudrun.domain.TStoreCard;
import com.tencent.wxcloudrun.domain.TUserCardApply;
import com.tencent.wxcloudrun.dto.UserCardListApplyResDTO;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Author ZhaoZiYin
 * @Date 2025/4/22
 * @Description 查询申请卡片列表请求出参
 */
@Data
@ApiModel(value = "UserCardListApplyResVo", description = "查询申请卡片列表请求出参")
public class UserCardListApplyResVo {

    @ApiModelProperty("用户卡片申请序号")
    private Long userCardApplyId;

    @ApiModelProperty("卡片序号")
    private Long cardId;

    @ApiModelProperty("卡片名称")
    private String cardName;

    @ApiModelProperty("卡片图片")
    private String imgUrl;

    @ApiModelProperty("用户ID")
    private Long userId;

    @ApiModelProperty("用户名称")
    private String userName;

    @ApiModelProperty("用户头像")
    private String userAvatar;

    @ApiModelProperty("申请状态")
    private Integer status;

    @ApiModelProperty("卡片数量")
    private Integer count;

    @ApiModelProperty("申请描述")
    private String memo;

    @ApiModelProperty("创建时间")
    private String createdTime;

    public void build(TUserCardApply apply, List<TStoreCard> cardList) {
        if (Objects.nonNull(apply)) {
            this.setUserCardApplyId(apply.getId());
            this.setCardId(apply.getCardId());
            TStoreCard storeCard = buildCard(apply.getCardId(), cardList);
            if (Objects.nonNull(storeCard)) {
                this.setCardName(storeCard.getName());
                this.setImgUrl(storeCard.getImgUrl());
            }
            this.setUserId(apply.getUserId());
            TUser user = RedisBusiUtil.getUserInfo(apply.getUserId());
            if (Objects.nonNull(user)) {
                this.setUserName(user.getName());
                this.setUserAvatar(user.getAvatar());
            }
            this.setStatus(apply.getStatus());
            this.setCount(apply.getCount());
            this.setMemo(apply.getMemo());
            this.setCreatedTime(apply.getCreatedTime().format(DateUtils.LOCAL_DATE_TIME_FORMATTER));
        }
    }

    private TStoreCard buildCard(Long cardId, List<TStoreCard> cardList) {
        if (!CollectionUtils.isEmpty(cardList)) {
            for (TStoreCard storeCard : cardList) {
                if (cardId.equals(storeCard.getId())) {
                    return storeCard;
                }
            }
        }
        return null;
    }

}
